
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import requests
from Ui_login import Ui_MainWindow1
from Ui_zhuce import Ui_MainWindow2
import json
import requests
import random

class Login(QtWidgets.QMainWindow,Ui_MainWindow1):
    def __init__(self,parent=None):
        super(Login,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(Qt. FramelessWindowHint)
        self.pushButton.clicked['bool'].connect(self.close)
        self.pushButton_2.clicked["bool"].connect(self.zhucet)
        self.pushButton_3.clicked["bool"].connect(self.denglu)
        self.setAttribute(Qt.WA_TranslucentBackground)
        user=str(FileChange("username",None))
        password=str(FileChange("password",None))
        sign=FileChange("sign",None)
        #判断是否保存账号，自动填写或自动登录
        if sign=="T":#刚刚注册
            self.lineEdit.setText(user)
            self.checkBox.setChecked(False)
            self.checkBox_2.setChecked(False)
        elif sign=="S":
            self.checkBox.setChecked(False)
            self.checkBox_2.setChecked(False)
        elif sign=="J":#记住密码
            self.checkBox.setChecked(True)
            self.checkBox_2.setChecked(False)
            self.lineEdit.setText(user)
            self.lineEdit_2.setText(password)
        elif sign=="Z":#自动登录
            self.pushButton_3.setEnabled(False)
            self.checkBox.setChecked(True)
            self.checkBox_2.setChecked(True)
            self.lineEdit.setText(user)
            self.lineEdit_2.setText(password)
            username=FileChange("username",None)
            password=FileChange("password",None)
            url="http://api.v6x.wht.dgwht.top/user/login"
            myobj = {"appid":"63e24dacc16dcb9e",
                        "username":username,
                        "password":password} 
            print(myobj)
            x = requests.post(url,json=myobj)
            FileChange("login_token",x.json()["data"]["login_token"])
            print(FileChange("login_token",None))
    #登录提交
    def denglu(self):
        username=self.lineEdit.text()
        password=self.lineEdit_2.text()

        url="http://api.v6x.wht.dgwht.top/user/login"
        myobj = {"appid":"63e24dacc16dcb9e",
                    "username":username,
                    "password":password} 
        print(myobj)
        x =requests.post(url,json=myobj)
        print(type(x.text))
        print(x.text)

        if eval(x.text)["code"] == 200:
            self.pushButton_3.setEnabled(False)
            en=eval(x.text)["data"]["login_token"]
            print(en)
            print("<succefully>")
            FileChange("login_token",en)
            if self.checkBox.isChecked() or self.checkBox.isChecked() == True:
                FileChange("sign","J")
                FileChange("username",username)
                FileChange("password",password)
            if self.checkBox.isChecked() == True:
                FileChange("sign","J")
        else:
            self.pushButton_3.setEnabled(True)
            self.label.setText("账号或密码错误")


    #登录注册页跳转
    def zhucet(self):
        ui2.show()
        ui1.close()
        FileChange("sign","F")

    def mousePressEvent(self, event):
        if event.button()==Qt.LeftButton:
            self.m_flag=True
            self.m_Position=event.globalPos()-self.pos() #获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  #更改鼠标图标     
    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:  
            self.move(QMouseEvent.globalPos()-self.m_Position)#更改窗口位置
            QMouseEvent.accept()     
    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag=False
        self.setCursor(QCursor(Qt.ArrowCursor))
        #注册页
class Zhuce(QtWidgets.QMainWindow,Ui_MainWindow2):
    def __init__(self,parent=None):
        super(Zhuce,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(Qt. FramelessWindowHint)
        self.pushButton_3.clicked['bool'].connect(self.zhucex)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.pushButton_5.clicked.connect(self.lower)
        self.pushButton_4.clicked.connect(self.close)
        self.pushButton_6.clicked.connect(self.yancode)
        self.pushButton_3.setEnabled(False)

        #发送验证码
    def yancode(self):
        acc=self.lineEdit.text()
        FileChange("acc",acc)
        url="http://api.v6x.wht.dgwht.top/yzm/send"
        myobj = {"appid":"63e24dacc16dcb9e",
        "acc":acc}
        x = requests.post(url,json=myobj)
        token=eval(x.text)["data"]["token"]
        FileChange("token",token)
        FileChange("msg",x.json()["msg"])
        if x.json()["code"]==200:
            self.pushButton_3.setEnabled(True)
            self.label.setText(FileChange("msg",None))
        else:
            self.label.setText(FileChange("msg",None))
    #提交注册
    def zhucex(self):
        FileChange("sign","S")
        username=random.randint(11111,999999999)
        FileChange("username",username)
        nickname=self.lineEdit_3.text()
        password=self.lineEdit_4.text()
        acc=self.lineEdit.text()
        email_code=self.lineEdit_2.text()

        url="http://api.v6x.wht.dgwht.top/user/reg"
        myobj = {"appid":"63e24dacc16dcb9e",
                    "username":username,
                    "password":password,
                    "nickname":nickname,
                    "acc":acc,
                    "email_code":email_code,
                    "email_code_token":FileChange("token",None)}
        print(myobj)
        x = requests.post(url,json=myobj)
        en=x.json()
        print(en)
        FileChange("ztcode",en["code"])

        if en["code"]==200:
            self.lineEdit_3.setFocus()
            self.lineEdit_3.clear()
            self.lineEdit_4.clear()
            self.lineEdit.clear()
            self.lineEdit_2.clear()
            self.lineEdit_5.clear()
            self.label.setText(FileChange("msg",None))
            FileChange("password",password)
            FileChange("sign","T")
            ui2.close()
            ui1.show()
        else:
            self.label.setText(FileChange("msg",None))

    #s鼠标拖动窗口
    def mousePressEvent(self, event):
        if event.button()==Qt.LeftButton:
            self.m_flag=True
            self.m_Position=event.globalPos()-self.pos() #获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))  #更改鼠标图标
            
    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:  
            self.move(QMouseEvent.globalPos()-self.m_Position)#更改窗口位置
            QMouseEvent.accept()
            
    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag=False
        self.setCursor(QCursor(Qt.ArrowCursor))

    #数据操作函数，str1为目标变更名称，str2为目标修改字符串，当str2为none时，返回str1的值
def FileChange(str1,str2):
    set_file=open("setting.txt","r+")
    set_file_dta=set_file.read()
    set_file.close()
    #print(set_file_dta)
    #print(type(set_file_dta))
    set_file_dta=eval(set_file_dta)
    if str2==None:
        return set_file_dta[str1]
    else:
        set_file_dta[str1]=str2
        file_data=open("setting.txt","w+")
        file_data.write(str(set_file_dta))        
    set_file.close()

    
if __name__=="__main__":
    app=QApplication(sys.argv)

    ui1=Login()
    ui2=Zhuce()
    ui1.show()
    sys.exit(app.exec())